<!--
 * @Author: daidai
 * @Date: 2022-03-04 09:23:59
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-05-07 11:05:02
 * @FilePath: \web-pc\src\pages\big-screen\view\indexs\index.vue
-->
<template>
<!--  v-if="isSuccess"  -->
  <div class="contents" v-if="isSuccess">
    <div class="contetn_left">
      <div class="pagetab">
         <div class="item" @click="jump">控制平台</div>
      </div>
      <ItemWrap class="contetn_left-top contetn_lr-item" title="河流宽度">
        <LeftTop :socketData="socketData"/>
      </ItemWrap>
      <ItemWrap class="contetn_left-center contetn_lr-item" title="平均速度">
        <LeftCenter :socketData="socketData"/>
      </ItemWrap>
      <ItemWrap
        class="contetn_left-bottom contetn_lr-item"
        title="中心面积"
        style="padding: 0 10px 16px 10px"
      >
        <LeftBottom :socketData="socketData"/>
      </ItemWrap>
    </div>
    <div class="contetn_center">
      <CenterMap class="contetn_center_top" />
      <ItemWrap class="contetn_center-bottom" title="河道监控">
        <CenterBottom />
      </ItemWrap>
    </div>
    <div class="contetn_right">
      <ItemWrap
        class="contetn_left-bottom contetn_lr-item"
        title="河流深度"
      >
        <RightTop :socketData="socketData"/>
      </ItemWrap>
      <ItemWrap
        class="contetn_left-bottom contetn_lr-item"
        title="最大流速"
        style="padding: 0 10px 16px 10px"
      >
        <RightCenter :socketData="socketData"/>
      </ItemWrap>
      <ItemWrap
        class="contetn_left-bottom contetn_lr-item"
        title="变化率"
      >
        <RightBottom :socketData="socketData"/>
      </ItemWrap>
    </div>
  </div>
</template>

<script>
import LeftTop from './left-top.vue'
import LeftCenter from "./left-center.vue";
import LeftBottom from "./left-bottom.vue";
import CenterMap from "./center-map.vue";
import CenterBottom from "./center-bottom.vue";
import RightTop from "./right-top.vue";
import RightCenter from "./right-center.vue";
import RightBottom from "./right-bottom.vue";
import dataValues from "@/assets/js/data_values.json"

export default {
  components: {
    LeftTop,
    LeftCenter,
    LeftBottom,
    CenterMap,
    RightTop,
    RightCenter,
    RightBottom,
    CenterBottom,
  },
  data() {
    return {
      websocket: null, // WebSocket对象
      reconnectInterval: 5000, // 重连间隔时间（毫秒）
      socketData:{
        "file_name1": null,
        "change_rate": null,
        "change_status": null,
        "max_depth": null,
        "avg_depth": null,
        "max_width": null,
        "avg_width": null,
        "hull_area": null,
        "average_speed": null,
        "max_speed": null,
        "flow": null
      },
      isSuccess: false,
    };
  },
  filters: {
    numsFilter(msg) {
      return msg || 0;
    },
  },
  beforeDestroy() {
    this.closeWebSocket(); // 在组件销毁前关闭WebSocket连接
  },
  created() {
    console.log(this.isSuccess,"isSuccess")
  },

  async mounted() {
    this.setupWebSocket(); // 创建WebSocket连接
  },
  methods: {
    jump(){
      this.$nextTick().then(() => {
        this.closeWebSocket();
        this.$store.state.setting.title = "监测控制平台";
        this.$router.push('lidar')
      });
    },
    setupWebSocket() {
      // this.websocket = new WebSocket("ws://8.218.248.25:8765"); // 创建WebSocket连接
      this.websocket = new WebSocket("ws://ofc.dmprima.cn:7010/ws"); // 创建WebSocket连接
      this.websocket.onopen = this.onWebSocketOpen; // WebSocket连接打开时的处理函数
      this.websocket.onmessage = this.onWebSocketMessage; // 收到WebSocket消息时的处理函数
      this.websocket.onclose = this.onWebSocketClose; // WebSocket连接关闭时的处理函数
    },
    closeWebSocket() {
      if (this.websocket) {
        this.websocket.close(); // 关闭WebSocket连接
      }
    },
    /**
     *  WebSocket连接打开后，启动心跳检测
     */
    onWebSocketOpen() {
      console.log("WebSocket connection is open");
      this.startHeartbeat();
    },
    // 处理从服务器接收的消息
    onWebSocketMessage(event) {
      if (event.data) {
        const message = JSON.parse(event.data);
        // console.log(message)
        //    根据业务来处理数据
        if(message.status){
          this.socketData.file_name1 = message.filename;
          this.socketData.change_status = message.status;
          this.socketData.change_rate = message.change_rate;
          this.socketData.max_depth = message.metrics.max_depth;
          this.socketData.avg_depth = message.metrics.avg_depth;
          this.socketData.max_width = message.metrics.max_width;
          this.socketData.avg_width = message.metrics.avg_width;
          this.socketData.hull_area = message.metrics.hull_area;
          this.socketData.average_speed = message.metrics.average_speed;
          this.socketData.max_speed = message.metrics.max_speed;
        }else{
          this.socketData.change_status = message.status;
          this.socketData.change_rate = message.change_rate;
        }
        this.isSuccess = true;
      }else{
        console.log("数据为空")
      }
    },
    onWebSocketClose() {
      console.log("WebSocket connection is closed");
      this.stopHeartbeat(); // WebSocket连接关闭时，停止心跳检测
      // setTimeout(this.setupWebSocket, this.reconnectInterval); // 在一定时间后重连WebSocket
    },
    sendMessage(message) {
      if (this.websocket && this.websocket.readyState === WebSocket.OPEN) {
        this.websocket.send(message); // 发送消息到WebSocket服务器
      }
    },
    startHeartbeat() {
      this.heartbeatInterval = setInterval(() => {
        if (this.websocket && this.websocket.readyState === WebSocket.OPEN) {
          this.websocket.send("ping"); // 发送心跳消息
        }
      }, 10000); // 每10秒发送一次心跳
    },
    stopHeartbeat() {
      if (this.heartbeatInterval) {
        clearInterval(this.heartbeatInterval); // 停止心跳检测定时器
      }
    },
    beforeDestroy() {
      this.closeWebSocket(); // 在组件销毁前关闭WebSocket连接
    },
  },
};
</script>
<style lang="scss" scoped>
// 内容
.contents {
  .contetn_left,
  .contetn_right {
    width: 540px;
    box-sizing: border-box;
    // padding: 16px 0;
  }

  .contetn_center {
    width: 720px;
  }

  //左右两侧 三个块
  .contetn_lr-item {
    height: 310px;
  }

  .contetn_center_top {
    width: 100%;
  }

  // 中间
  .contetn_center {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
  }

  .contetn_center-bottom {
    height: 315px;
  }

  //左边 右边 结构一样
  .contetn_left,
  .contetn_right {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    position: relative;


  }

  .contetn_left{
    .pagetab{
      cursor: pointer;
    }
  }
}


@keyframes rotating {
    0% {
        -webkit-transform: rotate(0) scale(1);
        transform: rotate(0) scale(1);
    }
    50% {
        -webkit-transform: rotate(180deg) scale(1.1);
        transform: rotate(180deg) scale(1.1);
    }
    100% {
        -webkit-transform: rotate(360deg) scale(1);
        transform: rotate(360deg) scale(1);
    }
}
</style>
